package com.red.wood.dubbo.task;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.dubbo.service.CdnBusinessDataService;

//@Service
public class CdnBusinessDubboTask implements Serializable{
	
	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(CdnBusinessDubboTask.class);
	
	@Autowired
	private CommonDao dao;
	
	@Autowired
    private CdnBusinessDataService service;
	
	public void doTask() {
		
		System.err.println("=====CDN业务任务开始=======");
		
		getCdnDataInfo();
		
		getCdnHouseInfo();
		
		getCdnIpInfo();
		
		getCdnNetInfo();
		
		getCdnCustomerInfo();
		
		getCdnDomainInfo();
		
		getCdnNodeInfo();
		
		getCdnLinkOfficerInfo();
		
		getCdnGetWayInfo();
		
		System.err.println("=====CDN业务任务结束=======");
	}
	
	private void getCdnGetWayInfo() {
		try {
			log.info("=============CDN链路信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnGetWayInfo(min, max);
				String sql = "insert into t_cdn_cdnnet_node_house_gateway(cdnId,cdnNetId,nodeId,houseId,bandWidth) values (?,?,?,?,?)";
				String columns = "CDNID,CDNNETID,NODEID,HOUSEID,BANDWIDTH";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_cdnnet_node_house_gateway");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN链路信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnLinkOfficerInfo() {
		try {
			log.info("============CDN-企业联系人信息插入开始==============");
			List<Map<String,Object>> list = service.getCdnLinkOfficerInfo();
			
			dao.deleteTableData("delete from t_cdn_linkofficerinfo_test");
			String sql = "insert into t_cdn_linkofficerinfo_test(cdnId,name,idType,mobile) values (?,?,?,?)";
			String columns = "CDNID,NAME,IDTYPE,MOBILE";
			dao.insertBatchTableData(list, sql, columns,10000);
			log.info("============CDN-企业联系人信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnNodeInfo() {
		try {
			log.info("=============CDN-子网对应节点信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnNodeInfo(min, max);
				String sql = "insert into t_cdn_cdnnetinfo_node_test(cdnId,cdnNetId,nodeId,nodeName) values (?,?,?,?)";
				String columns = "CDNID,CDNNETID,NODEID,NODENAME";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_cdnnetinfo_node_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-子网对应节点信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnDomainInfo() {
		try {
			log.info("============CDN-域名信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnDomainInfo(min, max);
				String sql = "insert into t_cdn_customer_service_domain_test(cdnId,customerId,serviceId,domainId,domain,regId,topDomain,timstamp) values (?,?,?,?,?,?,?,?)";
				String columns = "CDNID,CUSTOMERID,SERVICEID,DOMAINID,DOMAIN,REGID,TOPDOMAIN,TIMSTAMP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_customer_service_domain_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-域名信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnCustomerInfo() {
		try {
			log.info("============CDN-客户信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnCustomerInfo(min, max);
				String sql = "insert into t_cdn_customerinfo_test(province,cdnId,customerId,unitName,unitNature,address,timstamp) values (?,?,?,?,?,?,?)";
				String columns = "PROVINCE,CDNID,CUSTOMERID,UNITNAME,UNITNATURE,ADDRESS,TIMSTAMP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_customerinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-客户信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnNetInfo() {
		try {
			log.info("============CDN-子网信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnNetInfo(min, max);
				String sql = "insert into t_cdn_cdnnetinfo_test(cdnId,cdnNetId,topDomain,regId,domainId,timstamp) values (?,?,?,?,?,?)";
				String columns = "CDNID,CDNNETID,TOPDOMAIN,REGID,DOMAINID,TIMSTAMP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_cdnnetinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-子网信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnIpInfo() {
		try {
			log.info("============CDN-IP段信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnIpInfo(min, max);
				String sql = "insert into t_cdn_cdnnet_node_house_ipseg_test(province,cdnId,cdnNetId,nodeId,houseId,startIp,endIp,type,timstamp) values (?,?,?,?,?,?,?,?,?)";
				String columns = "PROVINCE,CDNID,CDNNETID,NODEID,HOUSEID,STARTIP,ENDIP,TYPE,TIMSTAMP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_cdnnet_node_house_ipseg_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-IP段信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}

	private void getCdnHouseInfo() {
		try {
			log.info("============CDN-机房信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getCdnHouseInfo(min, max);
				String sql = "insert into t_cdn_cdnnet_node_house_test(cdnId,cdnNetId,nodeId,houseId,houseName,houseType,houseProvince,houseCity,houseCounty,houseAdd,timstamp) values (?,?,?,?,?,?,?,?,?,?,?)";
				String columns = "CDNID,CDNNETID,NODEID,HOUSEID,HOUSENAME,HOUSETYPE,HOUSEPROVINCE,HOUSECITY,HOUSECOUNTY,HOUSEADD,TIMSTAMP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_cdn_cdnnet_node_house_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============CDN-机房信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getCdnDataInfo() {
		try {
			log.info("============CDN-企业信息插入开始==============");
			List<Map<String,Object>> list = service.getCdnDataInfo();
			
			dao.deleteTableData("delete from t_cdn_cdndata_test");
			String sql = "insert into t_cdn_cdndata_test(province,cdnId,cdnName,cdnAdd,corp,timstamp) values (?,?,?,?,?,?)";
			String columns = "PROVINCE,CDNID,CDNNAME,CDNADD,CORP,TIMSTAMP";
			dao.insertBatchTableData(list, sql, columns,10000);
			log.info("============CDN-企业信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
}
